package com.linewell.service.impl;

import com.linewell.dao.ArticleMapper;
import com.linewell.dao.FocusMapper;
import com.linewell.dao.LetterMapper;
import com.linewell.entity.Article;
import com.linewell.entity.ArticleVo;
import com.linewell.entity.Tag;
import com.linewell.service.IArticleService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/1/17 13:44
 */
@Service
public class ArticleService  implements IArticleService {

    @Autowired
    ArticleMapper articleDao;

    @Autowired
    LetterMapper letterDao;

    @Autowired
    FocusMapper focusDao;

    public List<Article> selectAllArticle() {
        return articleDao.selectAllArticle();
    }

    public List<Article> selectArticleWithUserId(Long userId) {
        return articleDao.selectArticleWithUserId(userId);
    }

    public Msg addArticle(Article article) {

        if (!articleDao.addArticle(article)){
            return Msg.fail().add("error","未知错误请重试");
        }else {
            Article article1 =articleDao.selectArticleByArticleId(article.articleId);
            return Msg.success().add("article",article1);
        }

    }

    public Msg updataArticleZan(Long articleId) {
        //articleDao.updataArcticleZan(articleId);
        return Msg.success();
    }

    public Article selectArticleWithArticleId(Long articleId) {
        return articleDao.selectArticleWithArticleId(articleId);
    }

    public List<Article> selectById(Long userId) {
        return articleDao.selectById(userId);
    }

    public List<Article> selectByTagId(Long tagId,int page) {
        return articleDao.selectByTagId(tagId,page);
    }

    public Msg selectBySort(Integer page, String sort) {
        List<Article> articleList = articleDao.selectBySort(page,sort);
        if (articleList.size() == 0){
            return Msg.fail();
        }else {
            return Msg.success().add("articleList",articleList);
        }
    }

    public Msg searchArticle(String searchType,String keyword,String page) {
        List<Article> searchArticleList = articleDao.searchArticle(searchType,keyword,Integer.valueOf(page));
        if (searchArticleList.size() == 0){
            return Msg.fail();
        } else {
            return Msg.success().add("searchArticleList",searchArticleList);
        }

    }

    public Msg getBestRank(int page) {
        List<Article> articleList = articleDao.getBestRank(page);
        if (articleList.size() > 0){
            return Msg.success().add("articleList",articleList);
        } else {
            return Msg.fail().add("msg","暂时没有信息");
        }
    }

    public List<ArticleVo> searchArticleKey(String articleKey) {
        return articleDao.searchArticleKey(articleKey);
    }

    public List<ArticleVo> searchAllArticle(int page,int limit ) {
        return articleDao.searchAllArticle(page,limit);
    }

    public Msg updataArticleCheck(Long articleId) {
        if (articleDao.updataArticleCheck(articleId,1)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }

    }

    public Msg deleteArticleByArticleId(Long articleId) {
        if (articleDao.deleteArticleByArticleId(articleId)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public int getCount() {
        return articleDao.getCount();
    }

    public Msg updateArticle(String field, String value,Long articleId) {
        if (articleDao.updateArticle(field,value,articleId)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg deleteAllArticle(String articleIds) {
        if (articleDao.deleteAllArticle(articleIds)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public int getCountIsCheck() {
        return articleDao.getCountIsCheck();
    }

    public List<ArticleVo> searchAllArticleIsCheck(int page, int limit) {
        return articleDao.searchAllArticleIsCheck(page,limit);
    }

    public Msg updateArticle1(Article article) {
        if(articleDao.updateArticle1(article)){
            letterDao.deleteLetterByArticle(article.articleId);
            return Msg.success();
        } else{
            return Msg.fail();
        }
    }

    public Msg recommendArticle(Long userId) {
        List<Tag> list = focusDao.selectUserTag(userId);
        List<Article> articles = new ArrayList<Article>();
        if (list.size() !=0){
            for (Tag tag:list) {
                articles.add(focusDao.recommendArticle(tag.getTagId()));
            }
        }
        int count = 10 - articles.size();
        List<Article> Alist =focusDao.selectAllArticle(count);
        for (Article a :Alist) {
            articles.add(a);
        }

        return Msg.success().add("articles",articles);
    }
}
